package com.lxp.base.net.builder;


import com.lxp.base.net.self.BaseCallBack;
import com.lxp.base.net.self.IBaseHttp;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by lxp on 2017/6/4.
 */

public abstract class BaseBuilder<T extends BaseBuilder> {
    protected String mUrl;
    protected Object mTag;
    protected Map<String, String> mHeaders;
    public boolean isCache = false;
    public int mCacheTime = 10;//缓存时间 分钟

    public IBaseHttp mHttp;

    /**
     * 缓存
     *
     * @param cache
     * @return
     */
    public T setCache(boolean cache) {
        isCache = cache;
        return (T) this;
    }

    /**
     * 缓存时间
     *
     * @param cacheTime
     * @return
     */
    public T setCacheTime(int cacheTime) {
        this.mCacheTime = cacheTime;
        return (T) this;
    }

    /**
     * 异步执行
     *
     * @param callBack 自定义回调
     */
    abstract void send(final BaseCallBack callBack);

    public BaseBuilder(IBaseHttp mHttp) {
        this.mHttp = mHttp;
    }

    /**
     * set url
     *
     * @param url url
     * @return
     */
    public T url(String url) {
        this.mUrl = url;
        return (T) this;
    }

    /**
     * set tag
     *
     * @param tag tag
     * @return
     */
    public T tag(Object tag) {
        this.mTag = tag;
        return (T) this;
    }

    /**
     * set headers
     *
     * @param headers headers
     * @return
     */
    public T headers(Map<String, String> headers) {
        this.mHeaders = headers;
        return (T) this;
    }

    /**
     * set one header
     *
     * @param key header key
     * @param val header val
     * @return
     */
    public T addHeader(String key, String val) {
        if (this.mHeaders == null) {
            mHeaders = new LinkedHashMap<>();
        }
        mHeaders.put(key, val);
        return (T) this;
    }


}
